{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "BlowOut_Predictor.ipynb",
      "provenance": [],
      "authorship_tag": "ABX9TyOwMheikaiVaeY0qJTqQIBZ",
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Divyanshu-ISM/Oil-and-Gas-data-analysis/blob/master/BlowOut_Predictor.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PKQuhbYaqMXb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Hello, In this model, we will be using a Mud weight versus fracture weight data set\n",
        "# and predict as and when the Primary well control is lost, and the chances of a \n",
        "# Blow Out shoot up. This can be used as a wake up call, to ask for Managed Pressure Drilling. "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "enOeWUKbrLU2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#The Algorithm used here - LOGISTIC REGRESSION"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "X_Y2ZPiSrRfE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "import seaborn as sns\n",
        "import pandas as pd\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xWYGkPFzrlfT",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "df = pd.read_excel('MPD.xlsx',sheet_name='Sheet3')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2HfTrjdqthjY",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 106
        },
        "outputId": "a0dca45d-92b8-4394-ec91-e495da7e92ba"
      },
      "source": [
        "df.head(2)"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/html": [
              "<div>\n",
              "<style scoped>\n",
              "    .dataframe tbody tr th:only-of-type {\n",
              "        vertical-align: middle;\n",
              "    }\n",
              "\n",
              "    .dataframe tbody tr th {\n",
              "        vertical-align: top;\n",
              "    }\n",
              "\n",
              "    .dataframe thead th {\n",
              "        text-align: right;\n",
              "    }\n",
              "</style>\n",
              "<table border=\"1\" class=\"dataframe\">\n",
              "  <thead>\n",
              "    <tr style=\"text-align: right;\">\n",
              "      <th></th>\n",
              "      <th>Depth</th>\n",
              "      <th>Pore pressure</th>\n",
              "      <th>Mud wt2</th>\n",
              "      <th>Frcture grd.</th>\n",
              "      <th>Overburden Grd.</th>\n",
              "      <th>B_O</th>\n",
              "    </tr>\n",
              "  </thead>\n",
              "  <tbody>\n",
              "    <tr>\n",
              "      <th>0</th>\n",
              "      <td>196.000000</td>\n",
              "      <td>1.030093</td>\n",
              "      <td>1.192130</td>\n",
              "      <td>1.203704</td>\n",
              "      <td>1.307870</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "    <tr>\n",
              "      <th>1</th>\n",
              "      <td>214.666667</td>\n",
              "      <td>1.307870</td>\n",
              "      <td>1.030093</td>\n",
              "      <td>1.197917</td>\n",
              "      <td>1.255787</td>\n",
              "      <td>1</td>\n",
              "    </tr>\n",
              "  </tbody>\n",
              "</table>\n",
              "</div>"
            ],
            "text/plain": [
              "       Depth   Pore pressure   Mud wt2  Frcture grd.  Overburden Grd.  B_O\n",
              "0  196.000000       1.030093  1.192130      1.203704         1.307870    1\n",
              "1  214.666667       1.307870  1.030093      1.197917         1.255787    1"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "r6AfwpSMtjEW",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "f559a913-7b66-48c2-eca8-28eb3265cb0a"
      },
      "source": [
        "# here B_O = 1 suggests we're safely within the pore grad- frac grad window. \n",
        "#      B_O = 0 means were outside the window and hence chances of Blow_Out.\n",
        "df.columns"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "Index(['Depth ', 'Pore pressure', 'Mud wt2', 'Frcture grd.', 'Overburden Grd.',\n",
              "       'B_O'],\n",
              "      dtype='object')"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "SVLOKZbFugAZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# declaring our features and label columns \n",
        "X = df[['Depth ', 'Pore pressure', 'Mud wt2', 'Frcture grd.', 'Overburden Grd.']]\n",
        "y = df[['B_O']]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G028fHNJu46t",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from sklearn.model_selection import train_test_split"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jiqy0FlVu9An",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=101)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5wg_ni13vpsD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Hence by now we have built and trained our Classification Model."
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tUm9TnoovF03",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from sklearn.linear_model import LogisticRegression"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yP9wNwrtvKhq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "logreg = LogisticRegression()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-pRh3IBXvN1Y",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 289
        },
        "outputId": "425cf6b1-a3f5-42f4-d4cc-dbd3e119cb55"
      },
      "source": [
        "logreg.fit(X_train,y_train)"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py:760: DataConversionWarning: A column-vector y was passed when a 1d array was expected. Please change the shape of y to (n_samples, ), for example using ravel().\n",
            "  y = column_or_1d(y, warn=True)\n",
            "/usr/local/lib/python3.6/dist-packages/sklearn/linear_model/_logistic.py:940: ConvergenceWarning: lbfgs failed to converge (status=1):\n",
            "STOP: TOTAL NO. of ITERATIONS REACHED LIMIT.\n",
            "\n",
            "Increase the number of iterations (max_iter) or scale the data as shown in:\n",
            "    https://scikit-learn.org/stable/modules/preprocessing.html\n",
            "Please also refer to the documentation for alternative solver options:\n",
            "    https://scikit-learn.org/stable/modules/linear_model.html#logistic-regression\n",
            "  extra_warning_msg=_LOGISTIC_SOLVER_CONVERGENCE_MSG)\n"
          ],
          "name": "stderr"
        },
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,\n",
              "                   intercept_scaling=1, l1_ratio=None, max_iter=100,\n",
              "                   multi_class='auto', n_jobs=None, penalty='l2',\n",
              "                   random_state=None, solver='lbfgs', tol=0.0001, verbose=0,\n",
              "                   warm_start=False)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zDve97OTvVp4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y_p = logreg.predict(X_test)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "k_D5S20NvjcU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#Now we have made our model's predictions. \n",
        "#lets check the accuracy!"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wrVJi-m0v0vD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "accu = logreg.score(X_test,y_test)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VA6r4LtEv9vo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a16a4260-75cb-42fa-c794-03e9a11b2d84"
      },
      "source": [
        "print(accu)"
      ],
      "execution_count": 20,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "0.9795918367346939\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pAkD7_imv_tf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "#BRILLIANT! That means our model has shown ~98% accuracy of predicting the correct scenario. "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hABKrIdKwKB3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# Practical Note - Such Huge accuracies are rarely obtained. This might be because \n",
        "# of some bias in the dataset, or maybe the small size of the dataset. \n",
        "# Any which way, we could learn how to make a Logistic Regression Model on\n",
        "# a Petroleum DataSet. "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JEbRGjB7w2DH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}